package com.peanut.devlibrary;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;

import com.mob.MobSDK;
import com.peanut.devlibrary.util.StringUtil;
import com.tencent.mars.xlog.Log;
import com.tencent.mars.xlog.Xlog;

import java.util.Iterator;
import java.util.List;

/**
 * Created by anonymouslystudio on 2017/6/5.
 */

public class LibInitManager {

    private Context mCtx;

    private boolean debug = false;
    private String logDir = "";

    private LibInitManager(){

    }
    /**
     *
     */
    private static class InstanceHolder{
        private static final LibInitManager instance = new LibInitManager();
    }

    /**
     *
     * @return
     */
    public static LibInitManager getInstance(){
        return InstanceHolder.instance;
    }


    public Context getContext(){
        return mCtx;
    }

    public void setDebug(boolean debug){
        this.debug = debug;
    }
    public void setLogDir(String folderPath){
        logDir = folderPath;
    }
    /**
     *
     * @param ctx
     */
    public void init(Context ctx){
        this.mCtx = ctx;
        MobSDK.init(ctx,"1f579e9d0b33c","3f93ac1ef3ba72219011ec6877ed3476");
        int pid = android.os.Process.myPid();
        String processAppName = getAppName(pid);
        initXlog(processAppName);
        if (processAppName == null
                || !processAppName.equalsIgnoreCase(mCtx.getPackageName())) {
            //this is sub process,just return
            return;
        }



    }

    /**
     *  init xlog
     * @param processName
     */
    private void initXlog(String processName){
        System.out.println("initXlog->processName: "+processName);
        if(StringUtil.isNullOrEmpty(logDir)){
            logDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/pkucarehm/log";

        }

        //init xlog
        if (debug) {
            Xlog.appenderOpen(Xlog.LEVEL_DEBUG, Xlog.AppednerModeAsync, "", logDir, "debug:"+processName);
            Xlog.setConsoleLogOpen(true);

        } else {
            Xlog.appenderOpen(Xlog.LEVEL_INFO, Xlog.AppednerModeAsync, "", logDir, "release:"+processName);
            Xlog.setConsoleLogOpen(false);
        }

        Log.setLogImp(new Xlog());

    }

    /**
     * return process name ,if this is main pricess ,return 'mainProcess'
     * instead of default package name
     * Note: process should has label ':'
     * @param pID
     * @return
     */
    private String getAppName(int pID) {
        System.out.println("getAppName-> pid: "+pID);
        String processName = null;
        ActivityManager am = (ActivityManager) mCtx.getSystemService(Context.ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        PackageManager pm = mCtx.getPackageManager();
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
            System.out.println("info: "+info.processName+" info.pid "+info.pid);
            try {
                if (info.pid == pID) {
                    if(info.processName.equals(mCtx.getPackageName())){
                        processName = "mainProcess";
                    }else{
                        processName = info.processName.split(":")[1];
                    }
                    return processName;
                }
            } catch (Exception e) {
                return "unknow";
            }
        }
        return processName;
    }


    /**
     *  close xlog when finish
     */
    public void closeXlog(){
        Log.appenderClose();

    }

    /**
     * xlog library
     */
    static{
        System.loadLibrary("stlport_shared");
        System.loadLibrary("marsxlog");
    }
}
